<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="application/x-blueprint+xml;charset=UTF-8" language="java" %>
<%@ page import="com.vhly.bpplat.twitter.HTTPClient" %>
<%@ page import="com.vhly.bpplat.twitter.model.User" %>
<%@ page import="com.vhly.bpplat.util.StreamUtil" %>
<%@ page import="org.kxml2.io.KXmlParser" %>
<%@ page import="org.kxml2.kdom.Document" %>
<%@ page import="org.kxml2.kdom.Element" %>
<%@ page import="org.xmlpull.v1.XmlPullParserException" %>
<%@ page import="java.io.ByteArrayInputStream" %>
<%@ page import="java.io.IOException" %>
<page xmlns:xsd="http://mobile.yahoo.com/widgets/schema/1.5/blueprint/">
    <content>
        <module>
            <header layout="simple">
                <layout-items>
                    <block class="title">TuiTe for Mobile</block>
                </layout-items>
            </header>
            <%
                String user = request.getParameter("u");
                String pass = request.getParameter("p");
                String ul = null;
                String so = null;
                if (user != null && user.length() > 0 && pass != null && pass.length() > 0) {
                    String ac = user + ":" + pass;
                    so = HTTPClient.request("http://api.twitter.com/1/account/verify_credentials.xml", ac, null, false);
//                    System.out.println("so = " + so);
                }
                if (so != null) {
                    session.setAttribute("user", user);
                    session.setAttribute("pass", pass);
                    session.setAttribute("login", "true");
                    Document dom = new Document();
                    KXmlParser parser = new KXmlParser();
                    byte[] buf = so.getBytes("UTF-8");
                    ByteArrayInputStream bin = null;
                    User usr;
                    try {
                        bin = new ByteArrayInputStream(buf);
                        parser.setInput(bin, null);
                        dom.parse(parser);
                        Element el = dom.getRootElement();
                        Element es = el.getElement("", "profile_image_url");
                        if (es != null) {
                            ul = (String) es.getChild(0);
                        }
                        dom = null;
                    } catch (XmlPullParserException xe) {

                    } catch (IOException ioe) {

                    } finally {
                        StreamUtil.close(bin);
                        bin = null;
                    }
            %>
                <%-- Login OK! Feature List--%>
            <%
                if(ul != null){
            %>
            <block><image resource="<%=ul%>"/></block>
            <%
                }
            %>
            <block>Hello <%=user%></block>
            <block>You Can:</block>
            <placard layout="simple" class="link">
                <layout-items>
                    <block class="title">Update Status(Tweet)</block>
                </layout-items>
                <load event="activate" resource="sendTweet.jsp"/>
            </placard>
            <placard layout="simple" class="link">
                <layout-items>
                    <block class="title">Friends Tweet</block>
                </layout-items>
                <load event="activate" resource="friendTweets.jsp"/>
            </placard>
            <placard layout="simple" class="link">
                <layout-items>
                    <block class="title">Search People</block>
                </layout-items>
                <load event="activate" resource="search.jsp"/>
            </placard>
            <%
            } else {
                // TODO Need login
            %>
            <block>No Login, Please login with your Twitter Account.</block>
            <%
                }
            %>
        </module>
    </content>
</page>